﻿using EYangFramework.Business.SystemManager.Interfaces;
using EYangFramework.Domain.Entitys.Entrys;
using EYangFramework.Domain.Repositorys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EYangFramework.Business.SystemManager.Implementation
{
    public class BLLUser : IBLLUser
    {
        IRepository<User> RepositoryUser;
        IRepository<Role> RepositoryRole;
        public BLLUser(IRepository<User> repositoryUser, IRepository<Role> repositoryRole)
        {
            this.RepositoryUser = repositoryUser;
            this.RepositoryRole = repositoryRole;
        }
        public bool DeleteUser(User user)
        {
            user.ISDELETE = true;
            return this.UpdateUser(user);
        }

        public void Dispose()
        {
            (RepositoryUser as Repository<User>).DbContext.Dispose();
        }

        public List<User> GetAllList()
        {
            return RepositoryUser.GetAllList().Where(u => !u.ISDELETE).ToList();
        }

        public User GetBusinessByID(Guid id)
        {
            return RepositoryUser.Get(id);
        }

        public Role GetRoleByName(string roleName)
        {
           return RepositoryUser.DbContext.Set<Role>().Where(t => t.ROLENAME == roleName).FirstOrDefault();
        }

        public List<string> GetRoleNameList()
        {
            return RepositoryRole.GetAllNoTracking().OrderBy(r => r.ROLENAME).Select(r => r.ROLENAME).ToList();
        }

        public User GetUserByName(string Name)
        {
            return RepositoryUser.GetAllList(u => u.USERNAME == Name).FirstOrDefault();
        }

        public bool InsertUser(User user)
        {
            RepositoryUser.Insert(user);
            return true;
        }

        public bool UpdateUser(User user)
        {
            RepositoryUser.Update(user);
            return true;
        }

    }
}
